import React, { Component } from "react";
import {
  View,
  ScrollView,
  DeviceEventEmitter,
  StatusBar,
  Image,
  NativeModules,
  Platform,
  Alert,
  Text,
  TouchableOpacity,
  ActivityIndicator
} from "react-native";
import { StackActions, NavigationActions } from "react-navigation";
import NavBarConfig from "../modules/NavBar";
import TabBar from "../modules/TabBar";
import Chart from "../modules/Chart";
import MemberBgt from "../modules/home/MemberBgt";
import NoticeList from "../modules/home/NoticeList";
import initJPush from "../../push";
import { UN_LOGIN } from "../../util/Const";
import { logOut } from "../../util/loginStateManager";
import http from "../../util/http";
import { BGT_VERSION } from "../../config/Api";
import Axios from "axios";
import SplashScreen from "react-native-splash-screen";
import { PullView } from "react-native-pull";

class Home extends Component {
  static navigationOptions = ({ navigation }) =>
    NavBarConfig(
      {
        // isSetNotShow: true,
        headerTitle: "BGT",
        headerLeft: (
          <TouchableOpacity
            onPress={() => {
              navigation.navigate("Barcode");
            }}
          >
            <View
              style={{
                flex: 1,
                position: "relative",
                marginLeft: 15,
                marginTop: 5
              }}
            >
              <Image
                style={{
                  resizeMode: "contain",
                  width: 25
                }}
                source={require("../../assets/qrcode-icon.png")}
              />
            </View>
          </TouchableOpacity>
        )
      },
      navigation
    );

  SplashScreenHide() {
    if (Platform.OS == "android") SplashScreen.hide();
  }

  async componentWillMount() {
    this.SplashScreenHide();
    try {
      // alert(Platform.OS);
      if (Platform.OS == "android") {
        let data = await Axios(
          "https://gitee.com/ilovenn/bgtapp/raw/master/android/download/version.json"
        ).then(data => {
          if (data.data.version !== "1.0.1") {
            Alert.alert("提示", data.data.versionInfo, [
              {
                text: "确定",
                onPress: () => {
                  NativeModules.upgrade.upgrade(
                    "https://github.com/drugsloveyou/bgtapp/raw/master/app-release.apk"
                  );
                }
              },
              {
                text: "取消",
                // onPress: () => ToastAndroid.show('Cancel', ToastAndroid.SHORT),
                style: "cancel"
              }
            ]);
          }
        });
      }
    } catch (e) {
      alert(e.message);
    }

    initJPush();
    this.subscrition = DeviceEventEmitter.addListener(UN_LOGIN, () => {
      logOut();
      const resetAction = StackActions.reset({
        index: 0,
        actions: [NavigationActions.navigate({ routeName: "Login" })]
      });
      this.props.navigation.dispatch(resetAction);
    });

    // alert(getLoginState());
    // beforeLoginHandle(this.props.navigation.navigate);
  }

  onPullRelease(resolve) {
    DeviceEventEmitter.emit("refresh");
    setTimeout(() => {
      resolve();
    }, 1000);
  }

  componentWillUnmount() {
    this.subscrition.remove();
  }

  render() {
    return (
      <View
        style={{
          top: 0,
          bottom: 0,
          left: 0,
          right: 0,
          position: "absolute"
        }}
      >
        <PullView
          onPullRelease={this.onPullRelease.bind(this)}
          style={{
            paddingBottom: 70
          }}
        >
          <MemberBgt navigation={this.props.navigation} />
          <NoticeList navigation={this.props.navigation} />
          <Chart />
          <View style={{ height: 70 }} />
        </PullView>
        <TabBar navigate={this.props.navigation.navigate} />
      </View>
    );
  }
}

export default Home;
